local linglong = fk.CreateSkill{
    name = "lingling__linglong",
    dynamic_desc = function (self, player, lang)
        local num = player:getMark("lingling__linglong")
        return "每局限（"..num.."）次，出牌阶段限一次，你可以从随机三张不同的普通锦囊牌中选择一张获得。你每使用两张装备牌，括号内数字便+1。"
    end,
}
Fk:loadTranslationTable{
    ["lingling__linglong"] = "玲珑",
    [":lingling__linglong"] = "每局限（1）次，出牌阶段限一次，你可以从随机三张不同的普通锦囊牌中选择一张获得。你每使用两张装备牌，括号内数字便+1。",
    ["#lingling__linglong-choose"] = "玲珑：获得其中一张牌",
}

linglong:addEffect("active",{
    anim_type = "drawcard",
    can_use = function (self, player)
        return (player:usedEffectTimes(self.name,Player.HistoryGame) < player:getMark("lingling__linglong")) and player:usedEffectTimes(self.name,Player.HistoryPhase) == 0
    end,
    card_num = 0,
    target_num = 0,
    card_filter = Util.FalseFunc,
    target_filter = Util.FalseFunc,
    on_use = function (self, room, effect)
        local player = effect.from
        local all_cards = table.simpleClone(room.draw_pile)
        table.insertTable(all_cards, room.discard_pile)
        local dat = {}
        for _, id in ipairs(all_cards) do
            local card = Fk:getCardById(id)
            if card:isCommonTrick() then
                dat[card.trueName] = dat[card.trueName] or {}
                table.insert(dat[card.trueName], id)
            end
        end
        local cards = {}
        while #cards < 3 and next(dat) ~= nil do
            local dicLength = 0
            for _, ids in pairs(dat) do
                dicLength = dicLength + #ids
            end

            local index = math.random(1, dicLength)
            dicLength = 0
            for name, ids in pairs(dat) do
                dicLength = dicLength + #ids
                if dicLength >= index then
                    table.insert(cards, ids[dicLength - index + 1])
                    dat[name] = nil
                    break
                end
            end
        end

        if #cards > 0 then
            local card = room:askToChooseCard(player,{
                skill_name = linglong.name,
                target = player,
                flag = { card_data = {
                    {"trick",cards}
                }},
                prompt = "#lingling__linglong-choose"
            })
            room:moveCards{
                ids = {card},
                to = player,
                toArea = Card.PlayerHand,
                moveReason = fk.ReasonJustMove,
                proposer = player,
                skillName = linglong.name,
            }
        end
    end,
    times = function (self, player)
        return player:getMark("lingling__linglong") - player:usedEffectTimes(self.name,Player.HistoryGame)
    end,
})
linglong:addEffect(fk.CardUsing,{
    can_trigger = function (self, event, target, player, data)
        return target == player and player:hasSkill(linglong.name) and data.card.type == Card.TypeEquip and player:getMark("lingling__linglong-temp") > 1
    end,
    on_cost = Util.TrueFunc,
    on_use = function (self, event, target, player, data)
        while player:getMark("lingling__linglong-temp") > 1 do
            player.room:removePlayerMark(player,"lingling__linglong-temp",2)
            player.room:addPlayerMark(player,linglong.name,1)
        end
    end,
    can_refresh = function (self, event, target, player, data)
        return target == player and player:hasSkill(linglong.name) and data.card.type == Card.TypeEquip
    end,
    on_refresh = function (self, event, target, player, data)
        player.room:addPlayerMark(player,"lingling__linglong-temp",1)
    end,
})
linglong:addAcquireEffect(function (self, player, is_start)
    if player:getMark(linglong.name) == 0 then
        player.room:setPlayerMark(player,linglong.name,1)
    end
end)

return linglong